package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysOpinion;
import com.ruoyi.project.system.service.ISysOpinionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 意见反馈Controller
 *
 * @author jjp
 * @date 2020-04-15
 */
@RestController
@RequestMapping("/system/opinion")
public class SysOpinionController extends BaseController {
    @Autowired
    private ISysOpinionService sysOpinionService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysMobileUserService sysMobileUserService;

    /**
     * 查询意见反馈列表
     */
    @PreAuthorize("@ss.hasPermi('system:opinion:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOpinion sysOpinion) {
        startPage();
        List<SysOpinion> list = sysOpinionService.selectSysOpinionList(sysOpinion);
        for (int i = 0; i < list.size(); i++) {
            Long userId = list.get(i).getUserId();
            String informationType = list.get(i).getInformationType();
            if (informationType.equals("2")) {
                SysUser sysUser = sysUserService.selectUserByUserId(userId);
                String userName = sysUser.getUserName();
                list.get(i).setUserName(userName);
            }
            if (informationType.equals("1")) {
                SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(userId);
                if(sysMobileUser.getUserName() == null ){
                    return getDataTable(list);
                }
                list.get(i).setUserName(sysMobileUser.getUserName());
            }
        }
        return getDataTable(list);
    }


    @PostMapping("/queryOrinion")
    public AjaxResult queryOrinion(@RequestBody SysOpinion sysOpinion) {
            return AjaxResult.success(sysOpinionService.selectDes(sysOpinion));
    }
}
